//
//  XHRLoginInfo.m
//  快医
//
//  Created by 张世敏 on 16/7/6.
//
//

#import "XHRLoginInfo.h"

@implementation XHRLoginInfo

/** 判断用户是否已经登录 **/
+(BOOL)validateIsLogined{
    
    // 从偏好设置中取登录标志,如果为yes,登录,为no,未登录
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    BOOL result = [defaults boolForKey:kIsLogined];
    
    return result;
}


/** 修改用户的登录状态(YES:已经登录,NO:未登录) **/
+(void)modifyLoginStatus:(BOOL)status{
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:status forKey:kIsLogined];
    
    [defaults synchronize];
}


/** 保存用户名和密码 **/
+(void)savePassword:(NSString *)password withUsername:(NSString *)username{
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[self base64Encode:username] forKey:kUSERNAME];
    [defaults setObject:[self base64Encode:password] forKey:kPASSWORD];
    
    // 同步登录状态
    [self modifyLoginStatus:YES];
    
    [defaults synchronize];
}


/** 获取用户名 **/
+(NSString *)getUsername{
    
    return [[NSUserDefaults standardUserDefaults] objectForKey:kUSERNAME];
}


/** 获取给定用户名对应的密码 **/
+(NSString *)getPassword{
    
    return [[NSUserDefaults standardUserDefaults] objectForKey:kPASSWORD];
}


/** 清空用户的登录信息 **/
+(void)clearUserInfo{
    
    [self savePassword:@"" withUsername:@""];
    // 同步修改登录状态
    [self modifyLoginStatus:NO];
}


/** base64编码 **/
+ (NSString *)base64Encode:(NSString *)str {
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    return [data base64EncodedStringWithOptions:0];
}


/** base64解码 **/
+ (NSString *)base64Decode:(NSString *)str {
    NSData *data = [[NSData alloc] initWithBase64EncodedString:str options:0];
    return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}


@end
